<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="medium"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="bnContent">
        <el-input
          v-model="queryParams.bnContent"
          placeholder="请输入轮播图内容"
          clearable
          @keyup.enter.native="handleQuery"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="medium" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="medium" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['index:banner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="5">
        <div class="inner-select">
          <el-select
            v-model="queryParams.bnIsShow"
            placeholder="请选择是否展示"
            size="small"
            @change="changeBnIsShow"
            clearable
          >
            <el-option
              v-for="dict in dict.type.banner_is_show"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList">
      <el-table-column label="轮播图" align="center" prop="bnUrl">
        <template slot-scope="scope">
          <el-image class="bannerImg" :src="scope.row.bnUrl" :preview-src-list="[scope.row.bnUrl]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="内容" align="center" prop="bnContent" />
      <el-table-column label="是否展示" align="center" prop="bnIsShow" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="选择展示的图片将会在客户端轮播图展示" placement="top">
            <dict-tag :options="dict.type.banner_is_show" :value="scope.row.bnIsShow" />
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['index:banner:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['index:banner:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改轮播图对话框 -->
    <el-dialog title="修改轮播图" :visible.sync="openEdit" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="内容">
          <el-input v-model="form.bnContent" placeholder="请输入轮播图内容" />
        </el-form-item>
        <el-form-item label="是否展示">
          <el-radio-group v-model="form.bnIsShow">
            <el-radio
              v-for="dict in dict.type.banner_is_show"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 上传轮播图弹框 -->
    <el-dialog title="上传轮播图" :visible.sync="openAdd" width="600px" append-to-body>
      <banner-upload :fileSize="3" @uploadFinish="uploadFinish" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listBanner,
  getBanner,
  delBanner,
  updateBanner
} from "@/api/index/banner";
import BannerUpload from "@/components/BannerUpload";
export default {
  name: "Banner",
  dicts: ["banner_is_show"],
  components: {
    BannerUpload
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 轮播图表格数据
      bannerList: [],
      // 是否显示修改
      openEdit: false,
      // 是否弹出添加
      openAdd: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bnContent: undefined,
        bnIsShow: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bnIsShow: [
          { required: true, message: "是否展示不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询轮播图列表 */
    getList() {
      this.loading = true;
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openEdit = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bnId: undefined,
        bnUrl: undefined,
        bnContent: undefined,
        bnIsShow: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryForm.bnIsShow = undefined;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.openAdd = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const bnId = row.bnId;
      getBanner(bnId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.openEdit = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          updateBanner(this.form)
            .then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openEdit = false;
              this.getList();
            })
            .finally(() => {
              this.buttonLoading = false;
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bnId = row.bnId;
      this.$modal
        .confirm('是否确认删除轮播图编号为" + bnId + "的数据项？')
        .then(() => {
          this.loading = true;
          return delBanner({ bnId: bnId });
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 更改select */
    changeBnIsShow() {
      this.getList();
    },
    /** 上传成功 */
    uploadFinish() {
      this.getList();``
      this.openAdd = false;
    }
  }
};
</script>
<style scoped>
.inner-select {
  display: flex;
  justify-content: center;
}
.inner-select span {
  font-size: 16px;
  color: #909399;
  line-height: 32px;
}

.bannerImg {
  display: block;
  margin: auto;
  width: 120px;
  height: 60px;
}
</style>